#include <i264e_common.h>
#ifdef DMALLOC
#include <imp/dmalloc.h>
#endif
void print_sps(i264e_t *h)
{
	#define PRINT_SPS(x, y) \
	printf("i_frame = %d sps->%-45s = %d\n", h->thread.ip.i_frame, x, sps->y);

	i264e_sps_t *sps = h->common.sps;
	PRINT_SPS("i_id", i_id)
	PRINT_SPS("i_profile_idc", i_profile_idc)
	PRINT_SPS("i_level_idc", i_level_idc)
	PRINT_SPS("b_constraint_set0", b_constraint_set0)
	PRINT_SPS("b_constraint_set1", b_constraint_set1)
	PRINT_SPS("b_constraint_set2", b_constraint_set2)
	PRINT_SPS("b_constraint_set3", b_constraint_set3)
	PRINT_SPS("i_log2_max_frame_num", i_log2_max_frame_num)
	PRINT_SPS("i_poc_type", i_poc_type)
	PRINT_SPS("i_log2_max_poc_lsb", i_log2_max_poc_lsb)
	PRINT_SPS("i_num_ref_frames", i_num_ref_frames)
	PRINT_SPS("b_gaps_in_frame_num_value_allowed", b_gaps_in_frame_num_value_allowed)
	PRINT_SPS("i_mb_width", i_mb_width)
	PRINT_SPS("i_mb_height", i_mb_height)
	PRINT_SPS("b_frame_mbs_only", b_frame_mbs_only)
	PRINT_SPS("b_mb_adaptive_frame_field", b_mb_adaptive_frame_field)
	PRINT_SPS("b_direct8x8_inference", b_direct8x8_inference)
	PRINT_SPS("b_crop", b_crop)


	PRINT_SPS("crop.i_left", crop.i_left)
	PRINT_SPS("crop.i_right", crop.i_right)
	PRINT_SPS("crop.i_top", crop.i_top)
	PRINT_SPS("crop.i_bottom", crop.i_top)

	PRINT_SPS("b_vui", b_vui)

	PRINT_SPS("vui.b_aspect_ratio_info_present", vui.b_aspect_ratio_info_present)
	PRINT_SPS("vui.i_sar_width", vui.i_sar_width)
	PRINT_SPS("vui.i_sar_height", vui.i_sar_height)
	PRINT_SPS("vui.b_overscan_info_present", vui.b_overscan_info_present)
	PRINT_SPS("vui.b_overscan_info", vui.b_overscan_info)
	PRINT_SPS("vui.b_signal_type_present", vui.b_signal_type_present)
	PRINT_SPS("vui.i_vidformat", vui.i_vidformat)
	PRINT_SPS("vui.b_fullrange", vui.b_fullrange)
	PRINT_SPS("vui.b_color_description_present", vui.b_color_description_present)
	PRINT_SPS("vui.i_colorprim", vui.i_colorprim)
	PRINT_SPS("vui.i_transfer", vui.i_transfer)
	PRINT_SPS("vui.i_colmatrix", vui.i_colmatrix)
	PRINT_SPS("vui.b_chroma_loc_info_present", vui.b_chroma_loc_info_present)
	PRINT_SPS("vui.i_chroma_loc_top", vui.i_chroma_loc_top)
	PRINT_SPS("vui.i_chroma_loc_bottom", vui.i_chroma_loc_bottom)
	PRINT_SPS("vui.b_timing_info_present", vui.b_timing_info_present)
	PRINT_SPS("vui.i_num_units_in_tick", vui.i_num_units_in_tick)
	PRINT_SPS("vui.i_time_scale", vui.i_time_scale)
	PRINT_SPS("vui.b_fixed_frame_rate", vui.b_fixed_frame_rate)
	PRINT_SPS("vui.b_nal_hrd_parameters_present", vui.b_nal_hrd_parameters_present)
	PRINT_SPS("vui.b_vcl_hrd_parameters_present", vui.b_vcl_hrd_parameters_present)

	PRINT_SPS("vui.hrd.i_cpb_cnt", vui.hrd.i_cpb_cnt)
	PRINT_SPS("vui.hrd.i_bit_rate_scale", vui.hrd.i_bit_rate_scale)
	PRINT_SPS("vui.hrd.i_cpb_size_scale", vui.hrd.i_cpb_size_scale)
	PRINT_SPS("vui.hrd.i_bit_rate_value", vui.hrd.i_bit_rate_value)
	PRINT_SPS("vui.hrd.i_cpb_size_value", vui.hrd.i_cpb_size_value)
	PRINT_SPS("vui.hrd.i_bit_rate_unscaled", vui.hrd.i_bit_rate_unscaled)
	PRINT_SPS("vui.hrd.i_cpb_size_unscaled", vui.hrd.i_cpb_size_unscaled)
	PRINT_SPS("vui.hrd.b_cbr_hrd", vui.hrd.b_cbr_hrd)
	PRINT_SPS("vui.hrd.i_initial_cpb_removal_delay_length", vui.hrd.i_initial_cpb_removal_delay_length)
	PRINT_SPS("vui.hrd.i_cpb_removal_delay_length", vui.hrd.i_cpb_removal_delay_length)
	PRINT_SPS("vui.hrd.i_time_offset_length", vui.hrd.i_time_offset_length)

	PRINT_SPS("vui.b_pic_struct_present", vui.b_pic_struct_present)
	PRINT_SPS("vui.b_bitstream_restriction", vui.b_bitstream_restriction)
	PRINT_SPS("vui.b_motion_vectors_over_pic_boundaries", vui.b_motion_vectors_over_pic_boundaries)
	PRINT_SPS("vui.i_max_bytes_per_pic_denom", vui.i_max_bytes_per_pic_denom)
	PRINT_SPS("vui.i_max_bits_per_mb_denom", vui.i_max_bits_per_mb_denom)
	PRINT_SPS("vui.i_log2_max_mv_length_horizontal", vui.i_log2_max_mv_length_horizontal)
	PRINT_SPS("vui.i_log2_max_mv_length_vertical", vui.i_log2_max_mv_length_vertical)
	PRINT_SPS("vui.i_num_reorder_frames", vui.i_num_reorder_frames)
	PRINT_SPS("vui.i_max_dec_frame_buffering", vui.i_max_dec_frame_buffering)

	PRINT_SPS("b_qpprime_y_zero_transform_bypass", b_qpprime_y_zero_transform_bypass)
	PRINT_SPS("i_chroma_format_idc", i_chroma_format_idc)

}

void log_sps(i264e_t *h)
{
	FILE  *pf = 0;
	pf = fopen("./sps.log", "a+");
	if (NULL == pf) {
		printf("ERROR: log_sps file open error!\n");
		return;
	}
	#define LOG_SPS(x, y) \
	fprintf(pf, "i_frame  %3d sps->%-45s  %d\n", h->thread.ip.i_frame, x, sps->y);

	i264e_sps_t *sps = h->common.sps;
	LOG_SPS("i_id", i_id)
	LOG_SPS("i_profile_idc", i_profile_idc)
	LOG_SPS("i_level_idc", i_level_idc)
	LOG_SPS("b_constraint_set0", b_constraint_set0)
	LOG_SPS("b_constraint_set1", b_constraint_set1)
	LOG_SPS("b_constraint_set2", b_constraint_set2)
	LOG_SPS("b_constraint_set3", b_constraint_set3)
	LOG_SPS("i_log2_max_frame_num", i_log2_max_frame_num)
	LOG_SPS("i_poc_type", i_poc_type)
	LOG_SPS("i_log2_max_poc_lsb", i_log2_max_poc_lsb)
	LOG_SPS("i_num_ref_frames", i_num_ref_frames)
	LOG_SPS("b_gaps_in_frame_num_value_allowed", b_gaps_in_frame_num_value_allowed)
	LOG_SPS("i_mb_width", i_mb_width)
	LOG_SPS("i_mb_height", i_mb_height)
	LOG_SPS("b_frame_mbs_only", b_frame_mbs_only)
	LOG_SPS("b_mb_adaptive_frame_field", b_mb_adaptive_frame_field)
	LOG_SPS("b_direct8x8_inference", b_direct8x8_inference)
	LOG_SPS("b_crop", b_crop)


	LOG_SPS("crop.i_left", crop.i_left)
	LOG_SPS("crop.i_right", crop.i_right)
	LOG_SPS("crop.i_top", crop.i_top)
	LOG_SPS("crop.i_bottom", crop.i_top)

	LOG_SPS("b_vui", b_vui)

	LOG_SPS("vui.b_aspect_ratio_info_present", vui.b_aspect_ratio_info_present)
	LOG_SPS("vui.i_sar_width", vui.i_sar_width)
	LOG_SPS("vui.i_sar_height", vui.i_sar_height)
	LOG_SPS("vui.b_overscan_info_present", vui.b_overscan_info_present)
	LOG_SPS("vui.b_overscan_info", vui.b_overscan_info)
	LOG_SPS("vui.b_signal_type_present", vui.b_signal_type_present)
	LOG_SPS("vui.i_vidformat", vui.i_vidformat)
	LOG_SPS("vui.b_fullrange", vui.b_fullrange)
	LOG_SPS("vui.b_color_description_present", vui.b_color_description_present)
	LOG_SPS("vui.i_colorprim", vui.i_colorprim)
	LOG_SPS("vui.i_transfer", vui.i_transfer)
	LOG_SPS("vui.i_colmatrix", vui.i_colmatrix)
	LOG_SPS("vui.b_chroma_loc_info_present", vui.b_chroma_loc_info_present)
	LOG_SPS("vui.i_chroma_loc_top", vui.i_chroma_loc_top)
	LOG_SPS("vui.i_chroma_loc_bottom", vui.i_chroma_loc_bottom)
	LOG_SPS("vui.b_timing_info_present", vui.b_timing_info_present)
	LOG_SPS("vui.i_num_units_in_tick", vui.i_num_units_in_tick)
	LOG_SPS("vui.i_time_scale", vui.i_time_scale)
	LOG_SPS("vui.b_fixed_frame_rate", vui.b_fixed_frame_rate)
	LOG_SPS("vui.b_nal_hrd_parameters_present", vui.b_nal_hrd_parameters_present)
	LOG_SPS("vui.b_vcl_hrd_parameters_present", vui.b_vcl_hrd_parameters_present)

	LOG_SPS("vui.hrd.i_cpb_cnt", vui.hrd.i_cpb_cnt)
	LOG_SPS("vui.hrd.i_bit_rate_scale", vui.hrd.i_bit_rate_scale)
	LOG_SPS("vui.hrd.i_cpb_size_scale", vui.hrd.i_cpb_size_scale)
	LOG_SPS("vui.hrd.i_bit_rate_value", vui.hrd.i_bit_rate_value)
	LOG_SPS("vui.hrd.i_cpb_size_value", vui.hrd.i_cpb_size_value)
	LOG_SPS("vui.hrd.i_bit_rate_unscaled", vui.hrd.i_bit_rate_unscaled)
	LOG_SPS("vui.hrd.i_cpb_size_unscaled", vui.hrd.i_cpb_size_unscaled)
	LOG_SPS("vui.hrd.b_cbr_hrd", vui.hrd.b_cbr_hrd)
	LOG_SPS("vui.hrd.i_initial_cpb_removal_delay_length", vui.hrd.i_initial_cpb_removal_delay_length)
	LOG_SPS("vui.hrd.i_cpb_removal_delay_length", vui.hrd.i_cpb_removal_delay_length)
	LOG_SPS("vui.hrd.i_time_offset_length", vui.hrd.i_time_offset_length)

	LOG_SPS("vui.b_pic_struct_present", vui.b_pic_struct_present)
	LOG_SPS("vui.b_bitstream_restriction", vui.b_bitstream_restriction)
	LOG_SPS("vui.b_motion_vectors_over_pic_boundaries", vui.b_motion_vectors_over_pic_boundaries)
	LOG_SPS("vui.i_max_bytes_per_pic_denom", vui.i_max_bytes_per_pic_denom)
	LOG_SPS("vui.i_max_bits_per_mb_denom", vui.i_max_bits_per_mb_denom)
	LOG_SPS("vui.i_log2_max_mv_length_horizontal", vui.i_log2_max_mv_length_horizontal)
	LOG_SPS("vui.i_log2_max_mv_length_vertical", vui.i_log2_max_mv_length_vertical)
	LOG_SPS("vui.i_num_reorder_frames", vui.i_num_reorder_frames)

	LOG_SPS("vui.i_max_dec_frame_buffering", vui.i_max_dec_frame_buffering)
	LOG_SPS("b_qpprime_y_zero_transform_bypass", b_qpprime_y_zero_transform_bypass)
	LOG_SPS("i_chroma_format_idc", i_chroma_format_idc)
	fclose(pf);

}

void print_pps(i264e_t *h)
{
	#define PRINT_PPS(x, y) \
	printf("i_frame = %d pps->%-40s = %d\n", h->thread.ip.i_frame, x, pps->y);
	#define PRINT_PPS_P(x, y) \
	printf("i_frame = %d pps->%-40s = %p\n", h->thread.ip.i_frame, x, pps->y);

	i264e_pps_t *pps = h->common.pps;

	PRINT_PPS("i_id", i_id)
	PRINT_PPS("i_sps_id", i_sps_id)
	PRINT_PPS("b_cabac", b_cabac)
	PRINT_PPS("b_pic_order", b_pic_order)
	PRINT_PPS("i_num_slice_groups", i_num_slice_groups)
	PRINT_PPS("i_num_ref_idx_l0_default_active", i_num_ref_idx_l0_default_active)
	PRINT_PPS("i_num_ref_idx_l1_default_active", i_num_ref_idx_l1_default_active)
	PRINT_PPS("b_weighted_pred", b_weighted_pred)
	PRINT_PPS("b_weighted_bipred", b_weighted_bipred)
	PRINT_PPS("i_pic_init_qp", i_pic_init_qp)
	PRINT_PPS("i_pic_init_qs", i_pic_init_qs)
	PRINT_PPS("i_chroma_qp_index_offset", i_chroma_qp_index_offset)
	PRINT_PPS("b_deblocking_filter_control", b_deblocking_filter_control)
	PRINT_PPS("b_constrained_intra_pred", b_constrained_intra_pred)
	PRINT_PPS("b_redundant_pic_cnt", b_redundant_pic_cnt)
	PRINT_PPS("b_transform_8x8_mode", b_transform_8x8_mode)
	PRINT_PPS("i_cqm_preset", i_cqm_preset)
	PRINT_PPS_P("scaling_list[0]", scaling_list[0])
	PRINT_PPS_P("scaling_list[1]", scaling_list[1])
	PRINT_PPS_P("scaling_list[2]", scaling_list[2])
	PRINT_PPS_P("scaling_list[3]", scaling_list[3])
	PRINT_PPS_P("scaling_list[4]", scaling_list[4])
	PRINT_PPS_P("scaling_list[5]", scaling_list[5])
	PRINT_PPS_P("scaling_list[6]", scaling_list[6])
	PRINT_PPS_P("scaling_list[7]", scaling_list[7])
}

void log_pps(i264e_t *h)
{
	FILE  *pf = 0;
	pf = fopen("./pps.log", "a+");
	if (NULL == pf) {
		printf("ERROR: log_pps file open error!\n");
		return;
	}
	#define LOG_PPS(x, y) \
	fprintf(pf, "i_frame  %3d  pps->%-40s  %d\n", h->thread.ip.i_frame, x, pps->y);
	#define LOG_PPS_P(x, y) \
	fprintf(pf, "i_frame  %3d  pps->%-40s  %p\n", h->thread.ip.i_frame, x, pps->y);

	i264e_pps_t *pps = h->common.pps;

	LOG_PPS("i_id", i_id)
	LOG_PPS("i_sps_id", i_sps_id)
	LOG_PPS("b_cabac", b_cabac)
	LOG_PPS("b_pic_order", b_pic_order)
	LOG_PPS("i_num_slice_groups", i_num_slice_groups)
	LOG_PPS("i_num_ref_idx_l0_default_active", i_num_ref_idx_l0_default_active)
	LOG_PPS("i_num_ref_idx_l1_default_active", i_num_ref_idx_l1_default_active)
	LOG_PPS("b_weighted_pred", b_weighted_pred)
	LOG_PPS("b_weighted_bipred", b_weighted_bipred)
	LOG_PPS("i_pic_init_qp", i_pic_init_qp)
	LOG_PPS("i_pic_init_qs", i_pic_init_qs)
	LOG_PPS("i_chroma_qp_index_offset", i_chroma_qp_index_offset)
	LOG_PPS("b_deblocking_filter_control", b_deblocking_filter_control)
	LOG_PPS("b_constrained_intra_pred", b_constrained_intra_pred)
	LOG_PPS("b_redundant_pic_cnt", b_redundant_pic_cnt)
	LOG_PPS("b_transform_8x8_mode", b_transform_8x8_mode)
	LOG_PPS("i_cqm_preset", i_cqm_preset)
	LOG_PPS_P("scaling_list[0]", scaling_list[0])
	LOG_PPS_P("scaling_list[1]", scaling_list[1])
	LOG_PPS_P("scaling_list[2]", scaling_list[2])
	LOG_PPS_P("scaling_list[3]", scaling_list[3])
	LOG_PPS_P("scaling_list[4]", scaling_list[4])
	LOG_PPS_P("scaling_list[5]", scaling_list[5])
	LOG_PPS_P("scaling_list[6]", scaling_list[6])
	LOG_PPS_P("scaling_list[7]", scaling_list[7])
	fclose(pf);
}

void print_slice_header(i264e_thread_t *t)
{
	int i = 0;
	int j = 0;
	char str[256];
	#define I_FRAME (t->ip.i_frame)
	#define PRINT_SLICE_HEADER(x, y) \
	printf("i_frame  %3d  sh->%-40s  %d\n", I_FRAME, x, sh->y);

    i264e_slice_header_t *sh = &(t->p.sh);

	PRINT_SLICE_HEADER("i_type", i_type)
	PRINT_SLICE_HEADER("i_first_mb", i_first_mb)
	PRINT_SLICE_HEADER("i_last_mb", i_last_mb)
	PRINT_SLICE_HEADER("i_pps_id", i_pps_id)
	PRINT_SLICE_HEADER("i_frame", i_frame)
	PRINT_SLICE_HEADER("b_mbaff", b_mbaff)
	PRINT_SLICE_HEADER("b_field_pic", b_field_pic)
	PRINT_SLICE_HEADER("b_bottom_field", b_bottom_field)
	PRINT_SLICE_HEADER("i_idr_pic_id", i_idr_pic_id)
	PRINT_SLICE_HEADER("i_poc", i_poc)
	PRINT_SLICE_HEADER("i_delta_poc_bottom", i_delta_poc_bottom)
	PRINT_SLICE_HEADER("i_delta_poc[0]", i_delta_poc[0])
	PRINT_SLICE_HEADER("i_delta_poc[1]", i_delta_poc[1])
	PRINT_SLICE_HEADER("i_redundant_pic_cnt", i_redundant_pic_cnt)
	PRINT_SLICE_HEADER("b_direct_spatial_mv_pred", b_direct_spatial_mv_pred)
	PRINT_SLICE_HEADER("b_num_ref_idx_override", b_num_ref_idx_override)
	PRINT_SLICE_HEADER("i_num_ref_idx_l0_active", i_num_ref_idx_l0_active)
	PRINT_SLICE_HEADER("i_num_ref_idx_l1_active", i_num_ref_idx_l1_active)
	PRINT_SLICE_HEADER("b_ref_pic_list_reordering[0]", b_ref_pic_list_reordering[0])
	PRINT_SLICE_HEADER("b_ref_pic_list_reordering[1]", b_ref_pic_list_reordering[1])

	for (i = 0; i < I264E_REF_MAX; i++) {
		sprintf(str, "ref_pic_list_order[0][%d].idc", i);
		printf("i_frame  %3d  sh->%-40s  %d\n", I_FRAME, str, sh->ref_pic_list_order[0][i].idc);
		sprintf(str, "ref_pic_list_order[1][%d].idc", i);
		printf("i_frame  %3d  sh->%-40s  %d\n", I_FRAME, str, sh->ref_pic_list_order[1][i].idc);
	}

	PRINT_SLICE_HEADER("b_weighted_pred", b_weighted_pred)

	for (i = 0; i < I264E_REF_MAX*2; i++) {
		for (j = 0; j < 3; j++) {
			sprintf(str, "weight[%d][%d].i_denom", i, j);
			printf("i_frame  %3d  sh->%-40s  %d\n", I_FRAME, str, sh->weight[i][j].i_denom);
			sprintf(str, "weight[%d][%d].i_scale", i, j);
			printf("i_frame  %3d  sh->%-40s  %d\n", I_FRAME, str, sh->weight[i][j].i_scale);
			sprintf(str, "weight[%d][%d].i_offset", i, j);
			printf("i_frame  %3d  sh->%-40s  %d\n", I_FRAME, str, sh->weight[i][j].i_offset);
			sprintf(str, "weight[%d][%d].weightfn", i, j);
			printf("i_frame  %3d  sh->%-40s  %p\n", I_FRAME, str, sh->weight[i][j].weightfn);
		}
	}

	PRINT_SLICE_HEADER("i_mmco_remove_from_end", i_mmco_remove_from_end)
	PRINT_SLICE_HEADER("i_mmco_command_count", i_mmco_command_count)

	for (i = 0; i < I264E_REF_MAX; i++) {
		sprintf(str, "mmco[%d].idc", i);
		printf("i_frame  %3d  sh->%-40s  %d\n", I_FRAME, str, sh->mmco[i].idc);
		sprintf(str, "mmco[%d].arg", i);
		printf("i_frame  %3d  sh->%-40s  %d\n", I_FRAME, str, sh->mmco[i].arg);
		sprintf(str, "mmco[%d].i_poc", i);
		printf("i_frame  %3d  sh->%-40s  %d\n", I_FRAME, str, sh->mmco[i].i_poc);
	}

	PRINT_SLICE_HEADER("i_cabac_init_idc", i_cabac_init_idc)
	PRINT_SLICE_HEADER("i_qp", i_qp)
	PRINT_SLICE_HEADER("i_qp_delta", i_qp_delta)
	PRINT_SLICE_HEADER("b_sp_for_swidth", b_sp_for_swidth)
	PRINT_SLICE_HEADER("i_qs_delta", i_qs_delta)
	PRINT_SLICE_HEADER("i_disable_deblocking_filter_idc", i_disable_deblocking_filter_idc)
	PRINT_SLICE_HEADER("i_alpha_c0_offset", i_alpha_c0_offset)
	PRINT_SLICE_HEADER("i_beta_offset", i_beta_offset)
}

